﻿using System;
using System.Windows.Input;
using WickedFlame.Core.Shapes;
using System.Windows;

namespace WickedFlame.Core.Tools
{
    public class HardSelectionTool : VectorTool
    {
        public HardSelectionTool()
            : base()
        {
        }

        RectangleShape selectionRectangle;
        public Rect Rectangle
        {
            get
            {
                if (selectionRectangle == null)
                    return new Rect(0,0,0,0);

                return selectionRectangle.Rectangle;
            }
        }

        public override void OnActivate(DrawingCanvas canvas)
        {
            canvas.SelectedLayer.UnselectAll();
        }

        public override void OnMouseLeftButtonDown(DrawingCanvas sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var point = Mouse.GetPosition(sender);

            if (selectionRectangle != null)
                sender.TopLayer.RemoveShape(selectionRectangle);
            //sender.RemoveTopLayerShape(selectionRectangle);

            selectionRectangle = new RectangleShape(point.X, point.Y, 1, 1);
            selectionRectangle.Fill = System.Windows.Media.Brushes.Transparent;
            selectionRectangle.Stroke = System.Windows.Media.Brushes.Black;
            selectionRectangle.StrokeThickness = 0.5;
            //selectionRectangle.StrokeDashArray.Add(5);
            //selectionRectangle.StrokeDashArray.Add(10);

            sender.TopLayer.AddShape(selectionRectangle);
        }

        public override void OnMouseMove(DrawingCanvas sender, System.Windows.Input.MouseEventArgs e)
        {
            //resize the selectionrectangle
            if (selectionRectangle != null && e.LeftButton == MouseButtonState.Pressed)
            {
                System.Windows.Point m = Mouse.GetPosition(sender);

                if (m.Y > (selectionRectangle.Y + (selectionRectangle.Height / 2)) &&
                    m.X > (selectionRectangle.X + (selectionRectangle.Width / 2)))//lower right
                    selectionRectangle.MoveTracker(m, 3);
                else if (m.Y > (selectionRectangle.Y + (selectionRectangle.Height / 2)) &&
                    m.X < (selectionRectangle.X + (selectionRectangle.Width / 2)))//lower left
                    selectionRectangle.MoveTracker(m, 4);
                else if (m.Y < (selectionRectangle.Y + (selectionRectangle.Height / 2)) &&
                    m.X < (selectionRectangle.X + (selectionRectangle.Width / 2)))//upper left
                    selectionRectangle.MoveTracker(m, 1);
                else//upper right
                    selectionRectangle.MoveTracker(m, 2);
            }
        }

        public override void OnMouseLeftButtonUp(DrawingCanvas sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (selectionRectangle != null)
            {
                selectionRectangle.Fill = new System.Windows.Media.SolidColorBrush(
                    System.Windows.Media.Color.FromArgb(
                    (byte)47,
                    (byte)35,
                    (byte)36,
                    (byte)77));
            }
        }

        public override void OnMouseRightButtonUp(DrawingCanvas sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (selectionRectangle != null)
            {
                sender.TopLayer.RemoveShape(selectionRectangle);
            }
        }

        public override void OnKeyDown(DrawingCanvas sender, System.Windows.Input.KeyEventArgs e)
        {
            if(e.Key == Key.Escape)
                if (selectionRectangle != null)
                {
                    sender.TopLayer.RemoveShape(selectionRectangle);
                }
        }

        public override void OnDeactivate(DrawingCanvas sender)
        {
            if (selectionRectangle != null)
            {
                sender.TopLayer.RemoveShape(selectionRectangle);
            }
        }
    }
}
